package com.kuang.demo.controller.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kuang.demo.bean.BaseRespVo;
import com.kuang.demo.bo.wx.WxDeleteAddressBO;
import com.kuang.demo.bo.wx.WxSaveDetailAddressBO;
import com.kuang.demo.mapper.wx.WXUserMapper;
import com.kuang.demo.mapper.wx.WxAddressMapper;
import com.kuang.demo.pojo.wx.Cskaoyanmall_address;
import com.kuang.demo.pojo.wx.Cskaoyanmall_user;
import com.kuang.demo.service.RegionService;
import com.kuang.demo.service.wx.WxAddressService;
import com.kuang.demo.vo.wx.WxQueryAddressVO;
import com.kuang.demo.vo.wx.WxQueryDetailAddressVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 郭浩 on 2020/11/4 16:13
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    WxAddressMapper wxAddressMapper;
    @Autowired
    WXUserMapper wxUserMapper;
    @Autowired
    WxAddressService wxAddressService;
    @Autowired
    RegionService regionService;

    @GetMapping("list")
    public BaseRespVo addressList(){
        Subject subject = SecurityUtils.getSubject();
        //拿到username 通过user表查到userid
        String username = (String) subject.getPrincipal();

        //mbp操作
        QueryWrapper<Cskaoyanmall_user> userwrapper = new QueryWrapper<>();
        userwrapper.select("id")
                .eq("username",username);
//        wxAddressMapper.selectList("id","name","address","mobile","is_default",)
        Cskaoyanmall_user cskaoyanmall_user = wxUserMapper.selectOne(userwrapper);
        Integer userId = cskaoyanmall_user.getId();

        //拿到userid之后，通过address表拿到地址
        QueryWrapper<Cskaoyanmall_address> addressWrapper = new QueryWrapper<>();
        addressWrapper.eq("user_id",userId).eq("deleted",0);
        List<Cskaoyanmall_address> addressList = wxAddressMapper.selectList(addressWrapper);

        System.out.println("33333333");
        //给结果赋值
        List<WxQueryAddressVO> list = new ArrayList<>();
        for (int i = 0; i < addressList.size(); i++) {
            //要根据id和userid查到地址范围
            Integer provinceId = addressList.get(i).getProvinceId();
            Integer cityId = addressList.get(i).getCityId();
            Integer areaId = addressList.get(i).getAreaId();
            String detailAddress = regionService.getDetailAddress(provinceId,cityId,areaId);

            WxQueryAddressVO wxQueryAddressVO = new WxQueryAddressVO();
            wxQueryAddressVO.setId(addressList.get(i).getId());
            wxQueryAddressVO.setIsDefault(addressList.get(i).getIsDefault());
            wxQueryAddressVO.setDetailedAddress(detailAddress + " " +addressList.get(i).getAddress());
            wxQueryAddressVO.setName(addressList.get(i).getName());
            wxQueryAddressVO.setMobile(addressList.get(i).getMobile());
            list.add(wxQueryAddressVO);
        }
        return BaseRespVo.ok(list);
    }

    @GetMapping("detail")
    public BaseRespVo detailAddress(int id){
        WxQueryDetailAddressVO  wxQueryDetailAddressVO = wxAddressService.queryDetailAddress(id);
        return BaseRespVo.ok(wxQueryDetailAddressVO);
    }

    @PostMapping("save")
    public BaseRespVo updateDetailAddress(@RequestBody WxSaveDetailAddressBO wxSaveDetailAddressBO){
        wxAddressService.updateDetailAddress(wxSaveDetailAddressBO);
        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    public BaseRespVo deleteDetailAddress(@RequestBody WxDeleteAddressBO wxDeleteAddressBO){
        wxAddressService.deleteDetailAddress(wxDeleteAddressBO);
        return BaseRespVo.ok();
    }
}
